//
//  ViewController.m
//  CollectionViewTest
//
//  Created by Snow on 16/8/2.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#import "SortCollectionViewCell.h"
#import "HeaderView.h"
#define kCell @"cell1"
#define kCell2 @"cell2"
#define kHeader @"header"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UICollectionViewFlowLayout *layoutOfSort = [[UICollectionViewFlowLayout alloc] init];
    layoutOfSort.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 2 - 10, 100);
    layoutOfSort.scrollDirection = UICollectionViewScrollDirectionVertical;
    layoutOfSort.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    layoutOfSort.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layoutOfSort];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    [collectionView registerClass:[SortCollectionViewCell class] forCellWithReuseIdentifier:kCell];
    [collectionView registerClass:[SortCollectionViewCell class] forCellWithReuseIdentifier:kCell2];
    [collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeader];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 6;
    } else {
        return 9;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        SortCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCell forIndexPath:indexPath];
        cell.backgroundColor = [UIColor blueColor];
        cell.imageOfPic.image = [UIImage imageNamed:@"14691424_1351245180941"];
        cell.label.text = @"美食";
        return cell;
    }
    else {
        SortCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCell2 forIndexPath:indexPath];
        cell.backgroundColor = [UIColor yellowColor];
        cell.imageOfPic.image = [UIImage imageNamed:@"u=2500309822,1225865711&fm=23&gp=0"];
        cell.label.text = @"美女";
        return cell;
    }
    return nil;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    if (indexPath.section == 0) {
        return CGSizeMake(screenWidth / 2 - 10 , 100);
    }else{
        return CGSizeMake(screenWidth / 3 - 15, 150);
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    NSArray *title = @[@"分类", @"美女"];
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
    HeaderView *reuse = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeader forIndexPath:indexPath];
        reuse.backgroundColor = [UIColor whiteColor];
        reuse.labelOfTitle.text = title[indexPath.section];
    return reuse;
    }
    return nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
